package com.wxy.controller;

import com.wxy.domain.MyException;
import com.wxy.domain.User;
import com.wxy.service.Impl.UserServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 47868
 */
@Controller
@RequestMapping("/springboot1")
public class controller1 {
    @Autowired
    @Qualifier("UserService")
    private UserServiceImpl userService;

    @GetMapping("getAll")
    @ResponseBody
    @Operation(summary = "getAll方法",description = "数据库的getAll")
    @ApiResponse(responseCode = "200",description = "获取成功", content = @Content)
    public List<User> getAll(){
        return userService.findAll();
    }

    @GetMapping("/getById/{id}")
    @ResponseBody
    @Operation(summary = "查询用户",description = "根据id查询用户")
    @ApiResponse(responseCode = "200",description = "查询成功", content = @Content)
    public User findById(@PathVariable("id") int id){
        User user = userService.findById(id);
        if(user != null){
            return user;
        }
        throw new MyException("没有该用户，请重试或联系管理员...");
    }

    @GetMapping("createError")
    @ResponseBody
    @Operation(summary = "制造一个错误",description = "制造一个错误检验自定义全局处理器是否正常运行")
    @ApiResponse(responseCode = "200",description = "成功跳转到error页面", content = @Content)
    public int createError(){
        int n=1/0;
        return n;
    }

    @GetMapping("success")
    @Operation(summary = "跳转",description = "跳转到success页面")
    @ApiResponse(responseCode = "200",description = "跳转成功", content = @Content)
    public String success(){
        return "success";
    }
}
